# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：RGB.py
#日期：2019-06-19
#备注：Python对RGB颜色与16进制颜色进行互转,然后创建对应颜色的图片并保存下来
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
from PIL import Image


# RGB格式颜色转换为16进制颜色格式
def RGB_to_Hex(rgb):
    RGB = rgb.split(',')            # 将RGB格式划分开来
    color = '#'
    for i in RGB:
        num = int(i)
        # 将R、G、B分别转化为16进制拼接转换并大写  hex() 函数用于将10进制整数转换成16进制，以字符串形式表示
        color += str(hex(num))[-2:].replace('x', '0').upper()
    print(color)
    return color


# 16进制颜色格式颜色转换为RGB格式
def Hex_to_RGB(hex):
    r = int(hex[1:3],16)
    g = int(hex[3:5],16)
    b = int(hex[5:7], 16)
    rgb = str(r)+','+str(g)+','+str(b)
    print(rgb)
    return rgb


def other_to_ten(number):
    two = bin(number)         # 10进制转换为2进制
    eight = oct(number)       # 10进制转换为8进制
    sixteen = hex(number)     # 10进制转换为16进制
    print(two,eight,sixteen)


# 创建90*35尺寸的颜色并保存
def new_image(color,outfile):
    new_image = Image.new("RGB", (90, 35), color)
    new_image.save(outfile)


# 获取所有的RGB颜色
def get_allRGB():
    rgb = []
    for i in range(0,256):
        for j in range(0,256):
            for c in range(0,256):
                rgb.append(str(i)+','+str(j)+','+str(c))
    return(rgb)


def main():
    color = RGB_to_Hex('255,127,2')
    outfile = 'color/' + str(color)+'.png'
    new_image(color,outfile)


if __name__ == '__main__':
    #main()
    #Hex_to_RGB('#FF7F02')
    other_to_ten(18)


